import urls from '/util/urls'
import request from '/util/request'
import canvas from '/util/canvas/canvas'
import * as jsapi from 'dingtalk-jsapi'
const app = getApp()

Page({
  ...canvas,
  data: {
    pageName: 'component/index',
    pageInfo: {
      pageId: 0,
    },
    waiting: true,
    openScanResult: false,
    scanResultText: '',
    day: 0,
    regular: 0,
    service: 0,
    sum: 0,
    isWorker: false
  },
  onShow(query) {
    request.login(this.init.bind(this))
  },
  init() {
    // this.getMyInfo()
    // this.getMyInstitutionInfo()
    this.setData({
      isWorker: app.globalData.userInfo.roles.filter(it=>it.role === 'worker').length > 0
    })
    this.getOperationGroups()
    this.getDayUnfinishedTask()
    this.getRegularUnfinishedTask()
    this.getServiceUnfinishedTask()
    this.getSummaryTask()
    this.setData({
      waiting: false
    })
  },
  getSummaryTask() {
    request.get(urls.questionnaireUnfinishedUrl("WORKER"), (data) => {
      this.setData({
        sum: data.length
      })
    })
  },
  getRegularUnfinishedTask() {
    request.get(urls.inspectionRegularUnfinishedTaskUrl(), (data) => {
      this.setData({
        regular: data
      })
    })
  },
  getDayUnfinishedTask() {
    request.get(urls.inspectionDayUnfinishedTaskUrl(), (data) => {
      this.setData({
        day: data
      })
    })
  },
  getServiceUnfinishedTask() {
    request.get(urls.inspectionServiceUnfinishedTaskUrl(), (data) => {
      this.setData({
        service: data.length
      })
    })

  },
  // getMyInstitutionInfo() {
  //   request.get(urls.myInstitutionInfo(), (data) => {
  //     app.globalData.instituteInfo = data
  //   })
  // },
  // getMyInfo() {
  //   request.get(urls.myInfo(), data => {
  //     app.globalData.userInfo = data
  //   })
  // },
  getOperationGroups() {
    request.get(urls.operationGroupsUrl(), data => {
      app.globalData.operationGroups = data
    })
  },
  onDailyReportItemTap(e) {
    dd.navigateTo({
      url: '/pages/daily/daily'
    })
  },
  onWeekReportItemTap(e) {
    dd.navigateTo({
      url: '/pages/week/week'
    })
  },
  onDayInspectionTap(e) {
    dd.navigateTo({
      url: '/pages/inspection/table/table?type=day'
    })
  },
  onRegularInspectionTap(e) {
    dd.navigateTo({
      url: '/pages/inspection/table/table?type=regular'
    })
  },
  onInsuranceInspectionTap(e) {
    dd.navigateTo({
      url: '/pages/inspection/table/table?type=insurance'
    })
  },
  onQuestionnaireTap(e) {
    dd.navigateTo({
      url: '/pages/questionnaire/questionnaire'
    })
  },
  onTap(e) {

  },
  onScanTap(e) {
    const that = this
    jsapi.biz.util.scan({
      type: 'all',
      onSuccess: function (data) {
        that.setData({
          openScanResult: true,
          scanResultText: data.text
        });
      },
      onFail: function (err) {
        that.setData({
          openScanResult: false,
          scanResultText: ''
        });
      }
    })
  },
  onModalClose() {
    this.setData({
      openScanResult: false,
      scanResultText: ''
    });
  }
});
